angular.module("app").directive("appCount", ["$interval", "$http", "utils", "userInfo", function($interval, $http, utils, userInfo) {
	return {
		restrict: 'AE',
		replace: true,
		templateUrl: 'views/template/count.html',
		scope: {
			codetype: "@", //短信类型
			seqnumber: "=", //短信流水号
			auto: "@", //是否执行一次
			tracode: "@", //交易代码，不传为070052
			ylphone: "@", //银行预留手机号
			bankid: "@", //银行卡号
			applyid: "=", //绑卡申请号
			phone: "@" //手机号
		},
		link: function(scope, ele, attr) {
			var temp = 0
			ele.bind("click", function() {
				temp++
				countDown()
			})
			if(!scope.auto) {
				countDown()
				ele.html("<span><em>60</em>秒</span>")
			}

			function countDown() {
				ele.attr("disabled", "disabled")
				var count = 60
				var countTime = $interval(function() {
					count--
					if(count === 0) {
						ele.html("获取验证码")
						ele.removeAttr("disabled")
						$interval.cancel(countTime)
					} else {
						ele.html("<span><em>" + count + "</em>秒</span>")
					}
				}, 1000)

				switch(scope.tracode) {
					case "070112":
						$http.get(utils.userSys, {
							params: {
								header: utils.header(scope.tracode),
								body: {
									acc_title: "",
									acc_tra_password: "",
									acc_type: "1",
									user_code: userInfo.user_code,
									acc_number: scope.bankid,
									bank_card_type_id: "1",
									bank_name: "",
									city_id: "",
									create_branch_name: "",
									province_id: "",
									reserve_phone_number: scope.ylphone

								}
							}
						}).then(function(res) {
							if(res.header.re_code == "0000") {
								scope.applyid = res.body.application_number
							}
						})
						break;
					case "000010":
						if(temp > 0) {
							$http.get(utils.tradeSys, {
								params: {
									header: utils.header("000010"),
									body: {
										order_id: userInfo.order_id,
										user_code: userInfo.user_code
									}
								}
							}).then(function(res) {
								if(res.header.re_code == "0000") {

								}

							})
						}

						break
					default:
						if(!scope.phone) {
							scope.phone = userInfo.phone
						}
						$http.get(utils.userSys, {
							params: {
								header: utils.header("070052"),
								body: {
									generate_code_type: scope.codetype,
									phone_number: scope.phone,
									user_code: ""
								}
							}
						}).then(function(res) {
							if(res.header.re_code == "0000") {
								scope.seqnumber = res.header.request_seq
							}
						})

				}

			}

		}

	}
}])